package com.weic.easycache.user;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import com.weic.easycache.framework.user.IUserConfig;

import android.os.Parcel;
import android.os.Parcelable;

@DatabaseTable
public class UserConfig implements IUserConfig {
	public static final String FIELD_USER_ID	= KEY_USER_ID;

	@DatabaseField(id = true, columnName = FIELD_USER_ID)
	public final long userId;
	/**头像本地路径**/
	@DatabaseField
	public final String logoPath;
	@DatabaseField
	public boolean allow3gDload = false;
	@DatabaseField
	public boolean allowBgDLoad = true;
	@DatabaseField
	public boolean useExtSdCard = true;
	@DatabaseField
	public boolean keepPassword = false;
	@DatabaseField
	public boolean autoLogin = false;

	public UserConfig() {
		userId = User.NO_ID;
		logoPath = null;
	}

	public UserConfig(long userId, String logoPath) {
		this.userId = userId;
		this.logoPath = logoPath;
	}

	@Override
	public long getUserId() {
		return userId;
	}

	@SuppressWarnings("unchecked")
	@Override
	public UserConfig invalidate() {
		return new UserConfig(User.NO_ID, null).copyFrom(this);
	}

	@SuppressWarnings("unchecked")
	@Override
	public UserConfig logout() {
		UserConfig uconf = invalidate();
		uconf.keepPassword = false;
		uconf.autoLogin = false;
		return uconf;
	}

	public UserConfig copyFrom(UserConfig conf) {
		allow3gDload = conf.allow3gDload;
		allowBgDLoad = conf.allowBgDLoad;
		useExtSdCard = conf.useExtSdCard;
		keepPassword = conf.keepPassword;
		autoLogin = conf.autoLogin;
		return this;
	}

	@Override
	public int describeContents() {
		return (int)userId;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeLong(userId);
		dest.writeString(logoPath);
		dest.writeInt(allow3gDload ? 1 : 0);
		dest.writeInt(allowBgDLoad ? 1 : 0);
		dest.writeInt(useExtSdCard ? 1 : 0);
		dest.writeInt(keepPassword ? 1 : 0);
		dest.writeInt(autoLogin ? 1 : 0);
	}

	public static final Parcelable.Creator<UserConfig> CREATOR = new Parcelable.Creator<UserConfig>() {
		public UserConfig createFromParcel(Parcel in) {
			return new UserConfig(in);
		}

		public UserConfig[] newArray(int size) {
			return new UserConfig[size];
		}
	};

	private UserConfig(Parcel in) {
		userId = in.readLong();
		logoPath = in.readString();
		allow3gDload = in.readInt() != 0;
		allowBgDLoad = in.readInt() != 0;
		useExtSdCard = in.readInt() != 0;
		keepPassword = in.readInt() != 0;
		autoLogin = in.readInt() != 0;
	}
}